from Tkinter import *
from math import pi, cos, sin
import datetime

class Darts:
    score = 3,19,7,16,8,11,14,9,12,5,20,1,18,4,13,6,10,15,2,17
    
class App(Tk):
            
    def score_cb(self, b):
        if not self.player:
            return
        v= b.cget('text')
        if v.endswith('D'):
            v=int(v[:-1])*2
        elif v.endswith('T'):
            v=int(v[:-1])*3
        else:
            v=int(v)
        p = self.player
        self.score[p] -= v
        if self.score[p]<0:
            print 'oh no'
        bg='white' if self.score[p]>0 else ('red' if self.score[p] else 'green')
        wcount = len(self.score_widgets[p])
        if wcount%3==0:
            text = '%s. %s (%s)' % (wcount+1, v, self.score[p])
        elif wcount%3==1:
            s1 = self.scores[p][-1]
            text = '%s. %s+%s = %s (%s)' %  (wcount+1, s1, v, s1+v, self.score[p])
        elif wcount%3==2:
            s1 = self.scores[p][-2]
            s2 = self.scores[p][-1]
            text = '%s. %s+%s+%s = %s (%s)' %  (wcount+1, s1, s2, v, s1+s2+v, self.score[p])
        l=Label(self.frame, text=text, width=20, bg=bg, anchor=W)
        w = self.player_widgets[p]
        l.place(x=w.winfo_x(), y=30+w.winfo_y()+20*(wcount//3))
        self.big_score[p].config(text=self.score[p])
        self.score_widgets[p].append(l)
        self.scores[p].append(v)
        if wcount%3 ==2:
            self.player_cb(self.other(p))

    def score_btn(self, text, width, bg, r, theta):
        b=Button(self.frame, text=text, width=width, bg=bg, font=('courier', 16, 'bold'))
        b.config(command=lambda _b=b: self.score_cb(_b))
        b.place(x=(200+370+r*-sin(theta)), y=(350+r*cos(theta)))

    def undo_cb(self, p):
        if not self.score_widgets[p]:
            return
        self.score_widgets[p].pop().place_forget()
        self.score[p] += self.scores[p].pop()
        self.big_score[p].config(text=self.score[p])

    def other(self,player):
        for p in self.player_widgets:
            if p!=player:
                return p
                
    def player_cb(self, p1):
        self.player=p1
        p2 = self.other(p1)
        self.big_score[p1].config(bg='green')
        self.big_score[p2].config(bg='white')
        self.player_widgets[p1].config(bg='green')
        self.player_widgets[p2].config(bg='white')

    def player_btn(self, name, start, x):
        self.score[name]=start
        self.big_score[name] = Button(text=start, font=('courier', 52, 'bold'), bg='white')
        self.big_score[name].config(command=lambda _1=name: self.player_cb(_1))
        self.big_score[name].place(x=x, y=10)
        nb=Button(text=name, width=5, bg='white')
        nb.place(x=x, y=80)
        nb.config(command=lambda _1=name: self.player_cb(_1))
        ub=Button(text='Undo', width=3, bg='blue')
        ub.place(x=x+70, y=80)
        ub.config(command=lambda _1=name: self.undo_cb(_1))
        self.player_widgets[name]= nb
        self.score_widgets[name] = []
        self.scores[name] = []

    def save_cmd(self):
        for s in self.score.values():
            if s==0:
                break
        else:
            return
        save_file = open('_darts_scores_.txt', 'a')
        print >>save_file, datetime.datetime.now()
        for p in self.scores:
            print >>save_file, p, self.scores[p]
        save_file.close()
        for p in self.scores:
            self.big_score[p].config(bg='white')
            self.player_widgets[p].config(bg='white')
            while self.scores[p]:
                self.undo_cb(p)
        self.player = None
        
    def __init__(self):
        self.player=None
        self.player_widgets = {}
        self.score = {}
        self.score_widgets = {}
        self.scores = {}
        self.big_score = {}
        
        main = Tk()
        self.frame=frame = Frame(main, bg='black',width=1200, height=750)
        frame.pack(expand=YES, fill=BOTH)
        for idx, s in enumerate(Darts.score):
            bg='green' if idx%2 else 'red'
            bg2='white' if idx%2 else 'black'
            fg2='black' if idx%2 else 'white'
            angle = 360/20*idx *pi/180
            self.score_btn(text='%sD'%s, width=3, bg=bg, r=340, theta=angle)
            self.score_btn(text='%s'%s, width=5, bg=bg, r=260, theta=angle)
            self.score_btn(text='%sT'%s, width=2, bg=bg, r=170, theta=angle)
            #self.score_btn(text='%s'%s, width=1, bg=bg, r=100, theta=angle)
        self.score_btn(text='25', width=3, bg='green', r=40, theta=pi)
        self.score_btn(text='50', width=3, bg='red', r=0, theta=0)
        self.score_btn(text='0', width=3, bg='white', r=40, theta=0)
        for angle in (pi/4, 3*pi/4, 5*pi/4, 7*pi/4):
            self.score_btn(text='0', width=6, bg='white', r=420, theta=angle)
        self.player_btn(name='Steve', start=501, x=50)
        self.player_btn(name='Ang', start=501, x=1000)
        b=Button(text='Save', bg='white', font=('courier', 52, 'bold'), command=self.save_cmd)
        b.place(x=10, y=650)

        main.mainloop()

App()
